/**
 * 修改密码
 */
Divo.app.ChangePasswordForm = function() {
	/*-------------- private 属性 ------------------*/
	var win, okBtn, tn = 1;
	var formPanel, fldNewPassword, fldNewPassword2;

	/* -------------------- private 方法 -------------------- */
	// 创建表单
	function createForm() {
		if (formPanel)
			return;

		fldNewPassword = new Ext.form.TextField( {
			fieldLabel : '新密码',
			id : 'newpassword',
			inputType : 'password',
			tabIndex : tn++
		});
		fldNewPassword.on('specialkey', onEnter);

		fldNewPassword2 = new Ext.form.TextField( {
			fieldLabel : '再输入新密码',
			id : 'newpassword2',
			inputType : 'password',
			tabIndex : tn++
		});
		fldNewPassword2.on('specialkey', onEnter);

		var config = {
			border : false,
			labelWidth : 150,
			labelPad : 5,
			bodyStyle : 'padding: 20px 50px',
			frame : false,
			items : [fldNewPassword, fldNewPassword2]
		};
		formPanel = new Ext.FormPanel(config);
	}

	// 创建窗口
	function createWindow() {
		if (win)
			return;

		win = new Ext.Window( {
			id : 'changepswd-win',
			title : '修改登录密码',
			//iconCls : 'icon-win',
			autoHeight : true,
			width : 450,
			layout : 'form',
			plain : false,
			buttonAlign : 'right',
			closable : false,
			modal : false,
			items : formPanel
		});

		win.on('show', function() {
			initFocus();
		});

		okBtn = win.addButton( {
			text : '确定',
			tabIndex : tn++
		}, onSubmit, this);
		
		cancelBtn = win.addButton( {
			text : '取消',
			tabIndex : tn++
		}, onWinHide, this);
	}

	// 隐藏窗口
	function onWinHide() {
		win.hide();
	}
	
	// 初始焦点
	function initFocus() {
		var f = Ext.get('newpassword');
		f.focus.defer(100, f);
	}

	// 提交
	function onSubmit() {
		var newPassword = fldNewPassword.getValue();
		var newPassword2 = fldNewPassword2.getValue();
		
		if (newPassword.length==0) {
			fldNewPassword.markInvalid("必须输入");
			return;
		}
		if (newPassword!==newPassword2) {
			fldNewPassword2.markInvalid("必须等于新密码");
			return;
		}

		var onChanged = function() {
		    okBtn.enable();
			Divo.say('密码修改成功。');
			win.hide();
		};
		okBtn.disable();
		Divo.wait("正在保存...");
		Divo.rmtCallAsync("sampleFacade.changePassword",onChanged,[newPassword]);
	}

	// 按回车键导航
	function onEnter(elem, e) {
		if (e.getKey() === e.ENTER) {
			var f = formPanel.form.items.itemAt(this.tabIndex);
			if (f) {
				f.focus();
			} else {
				okBtn.focus();
			}
			e.stopEvent();
		}
	}

	/* -------------------- public方法 -------------------- */
	return {

		showDialog : function() {
			createForm();
			createWindow();
			win.show();
		}
	};

}();

// EOP

